package com.example.onlinemarket.listener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;


import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager2.widget.ViewPager2;

import com.example.onlinemarket.R;
import com.example.onlinemarket.activity.OrderActivity;
import com.example.onlinemarket.util.ActivityUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

import static com.example.onlinemarket.util.ActivityUtil.contextActivity;
import static com.example.onlinemarket.util.ActivityUtil.goOrder;

public class NavigationItemSelectedListener implements
        NavigationView.OnNavigationItemSelectedListener,
        BottomNavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawer;
    private Menu sideMenu;
    private Menu navMenu;
    private LinearLayout searchBar;
    private NavigationView horizontalNavView;
    private BottomNavigationView navView;
    private ViewPager2 viewPager;

    //记录当前页面
    private int status = 0;
    private static final int INDEX_STATUS = 0;
    private static final int SHOPPING_CART_STATUS = 1;
    private static final int USER_INFO_STATUS = 2;
    private static final int ORDERS_STATUS = 3;

    public NavigationItemSelectedListener(LinearLayout searchBar, DrawerLayout drawer, NavigationView horizontalNavView, BottomNavigationView navView, ViewPager2 viewPager) {
        this.searchBar = searchBar;
        this.drawer = drawer;
        this.horizontalNavView = horizontalNavView;
        this.sideMenu = horizontalNavView.getMenu();
        this.navView = navView;
        this.navMenu = navView.getMenu();
        this.viewPager = viewPager;
    }

    @SuppressLint("WrongConstant")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        drawer.closeDrawer(Gravity.START);
        int id = item.getItemId();
        if(id == R.id.menu_item_index) {//跳转到主页
            showIndex();
            viewPager.setCurrentItem(INDEX_STATUS);
        }
        else if(id == R.id.menu_item_shoppingcart){//查看购物车
            showShoppingCart();
            viewPager.setCurrentItem(SHOPPING_CART_STATUS);
        }
        else if(id == R.id.menu_item_orders) {//查看订单
            showOrders();
        }
        else if(id == R.id.menu_item_user_info) {//查看用户信息
            showUserInfo();
            viewPager.setCurrentItem(USER_INFO_STATUS);
        }
        else if(id == R.id.menu_item_exit)//退出程序
            ActivityUtil.removeAll();
        return true;
    }

    //跳转到订单页面
    @SuppressLint("WrongConstant")
    private void showOrders() {
        drawer.closeDrawer(Gravity.START);
        if(status!=ORDERS_STATUS){
            enableItem();
            status = ORDERS_STATUS;
            goOrder((Activity)drawer.getContext(), OrderActivity.UNSENT_STATUS);
            disableItem();
        }
    }

    //跳转到购物车
    @SuppressLint("WrongConstant")
    public void showShoppingCart() {
        drawer.closeDrawer(Gravity.START);
        if(status!=SHOPPING_CART_STATUS){
            enableItem();
            searchBar.setVisibility(View.GONE);
            status = SHOPPING_CART_STATUS;
            setNormalNavUi();
            disableItem();
        }
    }

    //跳转到主页面
    public void showIndex(){
        if(status!=INDEX_STATUS){
            enableItem();
            searchBar.setVisibility(View.VISIBLE);
            status = INDEX_STATUS;
            setNormalNavUi();
            disableItem();
        }
    }

    //跳转到个人信息页面
    @SuppressLint("WrongConstant")
    public void showUserInfo(){
        drawer.closeDrawer(Gravity.START);
        if(status!=USER_INFO_STATUS){
            enableItem();
            searchBar.setVisibility(View.GONE);
            status = USER_INFO_STATUS;
            viewPager.setCurrentItem(USER_INFO_STATUS);
            setUserInfoNavUi();
            disableItem();
        }
    }

    //设置普通导航栏UI
    public void setNormalNavUi(){
        horizontalNavView.setBackgroundColor(contextActivity.getResources().getColor(R.color.pink_100));
        navView.setBackground(ContextCompat.getDrawable(navView.getContext(), R.color.white));
        navView.getMenu().getItem(INDEX_STATUS).setIcon(R.drawable.ic_home);
        navView.getMenu().getItem(SHOPPING_CART_STATUS).setIcon(R.drawable.ic_shoppingcart);
        navView.getMenu().getItem(USER_INFO_STATUS).setIcon(R.drawable.ic_user);
        navView.setItemTextColor(ColorStateList.valueOf(contextActivity.getResources().getColor(R.color.black)));
    }

    //设置用户信息界面下的导航栏UI
    public void setUserInfoNavUi(){
        horizontalNavView.setBackground(ContextCompat.getDrawable(drawer.getContext(), R.mipmap.login_bg));
        navView.setBackground(ContextCompat.getDrawable(navView.getContext(), R.color.nav_bar_user_info));
        navView.getMenu().getItem(INDEX_STATUS).setIcon(R.drawable.ic_home_white);
        navView.getMenu().getItem(SHOPPING_CART_STATUS).setIcon(R.drawable.ic_shoppingcart_white);
        navView.getMenu().getItem(USER_INFO_STATUS).setIcon(R.drawable.ic_user_white);
        navView.setItemTextColor(ColorStateList.valueOf(contextActivity.getResources().getColor(R.color.white)));
    }

    //启用之前的选项
    public void enableItem(){
        switch (status){
            case INDEX_STATUS:
                sideMenu.getItem(INDEX_STATUS).setChecked(false);//设为主页按钮选中
                navMenu.getItem(INDEX_STATUS).setChecked(false);
                sideMenu.getItem(INDEX_STATUS).setEnabled(true);//禁用主页按钮
                navMenu.getItem(INDEX_STATUS).setEnabled(true);
                break;
            case SHOPPING_CART_STATUS:
                sideMenu.getItem(SHOPPING_CART_STATUS).setChecked(false);//设为购物车按钮选中
                navMenu.getItem(SHOPPING_CART_STATUS).setChecked(false);
                sideMenu.getItem(SHOPPING_CART_STATUS).setEnabled(true);//禁用购物车按钮
                navMenu.getItem(SHOPPING_CART_STATUS).setEnabled(true);
                break;
            case ORDERS_STATUS:
                sideMenu.getItem(2).setChecked(false);//设为订单按钮选中
                sideMenu.getItem(2).setEnabled(true);//禁用订单按钮
                break;
            case USER_INFO_STATUS:
                navMenu.getItem(USER_INFO_STATUS).setChecked(false);//设为个人信息按钮选中
                navMenu.getItem(USER_INFO_STATUS).setEnabled(true);//禁用个人信息按钮
                break;
            default:
                break;
        }
    }

    //禁用选中的选项，避免重复选择
    public void disableItem(){
        switch (status){
            case INDEX_STATUS:
                sideMenu.getItem(INDEX_STATUS).setChecked(true);//设为主页按钮选中
                navMenu.getItem(INDEX_STATUS).setChecked(true);
                sideMenu.getItem(INDEX_STATUS).setEnabled(false);//禁用主页按钮
                navMenu.getItem(INDEX_STATUS).setEnabled(false);
                break;
            case SHOPPING_CART_STATUS:
                sideMenu.getItem(SHOPPING_CART_STATUS).setChecked(true);//设为购物车按钮选中
                navMenu.getItem(SHOPPING_CART_STATUS).setChecked(true);
                sideMenu.getItem(SHOPPING_CART_STATUS).setEnabled(false);//禁用购物车按钮
                navMenu.getItem(SHOPPING_CART_STATUS).setEnabled(false);
                break;
            case ORDERS_STATUS:
                sideMenu.getItem(2).setChecked(true);//设为订单按钮选中
                sideMenu.getItem(2).setEnabled(false);//禁用订单按钮
                break;
            case USER_INFO_STATUS:
                navMenu.getItem(USER_INFO_STATUS).setChecked(true);//设为个人信息按钮选中
                navMenu.getItem(USER_INFO_STATUS).setEnabled(false);//禁用个人信息按钮
                break;
            default:
                break;
        }
    }
}
